import matplotlib

matplotlib.use('TkAgg')
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from utils.config import Config
from collections import Counter

matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['axes.unicode_minus'] = False

# 类别映射表
cat_id2label = {
    0: "书籍", 1: "平板", 2: "手机", 3: "水果", 4: "洗发水",
    5: "热水器", 6: "蒙牛", 7: "衣服", 8: "计算机", 9: "酒店"
}
label_map = {0: "负面", 1: "正面"}

root_path = "D:/pycode/group4_nlp_project"
config = Config(root_path)
img_path = config.img_path
# data_path = config.fast_file
data_path = config.clean_file


def data_EDA():
    # 读取数据
    data = pd.read_csv(data_path)

    # 将类别ID转换为中文名称
    data['cat_name'] = data['cat'].map(cat_id2label)
    # 将标签转换为中文名称
    data['label_name'] = data['label'].map(label_map)

    print("数据前5行：")
    print(data.head())

    print("\n数据基本信息：")
    data.info()  # 查看数据类型、列数、行数及缺失值情况

    print("\n数据统计描述：")
    print(data.describe())  # 数值型列的统计描述，这里主要是label列

    # 检查缺失值
    print("\n缺失值情况：")
    print(data.isnull().sum())

    # 查看类别（cat列）的分布
    print("\n类别分布：")
    cat_counts = data['cat_name'].value_counts()
    print(cat_counts)
    # 绘制类别分布柱状图并保存
    plt.figure(figsize=(10, 6))
    sns.countplot(x='cat_name', data=data, order=cat_counts.index)
    plt.title('类别分布')
    plt.xticks(rotation=45)
    plt.tight_layout()  # 调整布局，防止标签被截断
    plt.savefig(img_path + 'clean_category_distribution.png', dpi=300)  # 保存图片
    plt.show()

    # 查看标签（label列）的分布
    print("\n标签分布：")
    label_counts = data['label_name'].value_counts()
    print(label_counts)
    # 绘制标签分布饼图并保存
    plt.figure(figsize=(6, 6))
    plt.pie(label_counts, labels=label_counts.index, autopct='%1.1f%%')
    plt.title('标签分布')
    plt.tight_layout()
    plt.savefig(img_path + 'clean_label_distribution.png', dpi=300)  # 保存图片
    plt.show()

    # 分析review_clean文本长度
    data['review_clean_length'] = data['review_clean'].apply(lambda x: len(str(x)))
    print("\nreview_clean文本长度统计：")
    print(data['review_clean_length'].describe())
    # 绘制review_clean文本长度分布直方图并保存
    plt.figure(figsize=(10, 6))
    sns.histplot(data['review_clean_length'], kde=True)
    plt.title('评论长度分布')
    plt.tight_layout()
    plt.savefig(img_path + 'clean_review_clean_length_distribution.png', dpi=300)  # 保存图片
    plt.show()

    # 查看不同类别下标签的分布
    print("\n不同类别下标签分布：")
    cat_label_counts = data.groupby(['cat_name', 'label_name']).size().unstack()
    print(cat_label_counts)
    # 绘制不同类别下标签分布的分组柱状图并保存
    cat_label_counts.plot(kind='bar', stacked=True, figsize=(12, 8))
    plt.title('不同类别下的标签分布')
    plt.xticks(rotation=45)
    plt.tight_layout()
    plt.savefig(img_path + 'clean_label_distribution_by_category.png', dpi=300)  # 保存图片
    plt.show()


if __name__ == '__main__':
    data_EDA()
